package com.jd.finance.core.controller.admin;

import com.jd.finance.core.service.*;
import com.jd.finance.core.service.impl.AdminUtilsServiceImpl;
import com.jd.finance.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "后台首页")
@Slf4j
@RestController
@RequestMapping("/admin/core/adminIndexCenter")
public class AdminIndexController {

    @Resource
    private LoanApplyService loanApplyService;

    @Resource
    private AdminUtilsServiceImpl adminUtilsServiceimpl;

    @Resource
    private UserService userService;

    @Resource
    private BankService bankService;

    @Resource
    private ProductService productService;

    @Resource
    private LoanService loanService;

    @Resource
    private PhotoService photoService;
    @Resource
    private AdminUtilsServiceImpl adminUtilsService;

    @ApiOperation("获取当月贷款申请金额总数")
    @GetMapping("/getSumLoanMonth")
    public BaseResult getSumLoanMonth(Integer uid) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isAdmin(uid)) {
            log.error("当前登录用户不是管理员账户");
            return BaseResult.error().message("当前登录用户不是管理员账户");
        }
        return loanApplyService.getSumLoanMonth(uid);
    }

    @ApiOperation("获取前台账号总数")
    @GetMapping("/getSumAccount")
    public BaseResult getSumAccount(Integer uid) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isAdmin(uid)) {
            log.error("当前登录用户不是管理员账户");
            return BaseResult.error().message("当前登录用户不是管理员账户");
        }
        return userService.getSumAccount(uid);
    }

    @ApiOperation("获取各银行当月贷款总金额")
    @GetMapping("/getBankLoanMonth")
    public BaseResult getBankLoanMonth(Integer uid) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isAdmin(uid)) {
            log.error("当前登录用户不是管理员账户");
            return BaseResult.error().message("当前登录用户不是管理员账户");
        }
        return bankService.getBankLoanMonth(uid);
    }

    @ApiOperation("获取贷款产品成交总金额前5名")
    @GetMapping("/getSumMoneyTopFivePro")
    public BaseResult getSumMoneyTopFivePro(Integer uid) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isAdmin(uid)) {
            log.error("当前登录用户不是管理员账户");
            return BaseResult.error().message("当前登录用户不是管理员账户");
        }
        return productService.getSumMoneyTopFivePro(uid);
    }

    @ApiOperation("获取各省份全年贷款总金额")
    @GetMapping("/getCityYearSumMoney")
    public BaseResult getCityYearSumMoney(Integer uid) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isAdmin(uid)) {
            log.error("当前登录用户不是管理员账户");
            return BaseResult.error().message("当前登录用户不是管理员账户");
        }
        return loanService.getCityYearSumMoney(uid);
    }

    @ApiOperation("更新腰线图片路径")
    @PostMapping("/updateLineSrc")
    public BaseResult updateLineSrc(Integer uid, Integer photoId, String url, HttpServletRequest request) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isInformation(uid)) {
            log.error("当前登录用户不是信息管理员账户 s");
            return BaseResult.error().message("当前登录用户不是信息管理员账户 s");
        }
        return photoService.updateLineSrc(uid, photoId, url, request);
    }

    @ApiOperation("根据地区名称，获取全年该地区贷款产品成交数量")
    @GetMapping("/getProductDealNumberByCity")
    public BaseResult getProductDealNumberByCity(Integer uid,String bankCity){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isAdmin(uid)){
            log.error("当前登录用户不是管理员账户");
            return BaseResult.error().message("当前登录用户不是管理员账户");
        }
        return bankService.getProductDealNumberByCity(uid,bankCity);
    }


    @ApiOperation("进行中的贷款列表（分页，每页6条）")
    @GetMapping("/getTradingLoanList/{page}/{limit}/{uid}")
    public BaseResult getTradingLoanList(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(value = "操作的uid", required = true)
            @PathVariable Integer uid) {
        if (uid == null) {
            log.error("id为空");
            return BaseResult.error().message("id为空");
        }
        if (adminUtilsService.isAdmin(uid)) {
            log.error("无访问权限");
            return BaseResult.ok().message("无访问权限");
        }

        return loanService.getTradingLoanList(page,limit,uid);

    }


}
